﻿using QFramework;
using UnityEngine;

namespace HKTools
{
    public class Test_UseSerialPortSystem : MonoBehaviour, IController
    {
        public IArchitecture GetArchitecture()
        {
            return HK_ArchitectureProvider.Architecture;
        }

#if NET_4_6
        void Start()
        {
            this.RegisterEvent<Event_ReceiveSerialPortData>(args =>
            {
                Debug.Log("接收到串口数据: " + args._serialPortData);
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }

        void Update()
        {
            if (Input.GetKeyDown(KeyCode.F1))
            {
                string[] ports = this.GetSystem<IHK_SerialPortSystem>().ScanPorts();

                foreach (string port in ports)
                {
                    Debug.Log(port);
                }

                // 初始化并开启 串口通信
                this.SendCommand(new CMD_OpenSerialPort(6));
            }

            if (Input.GetKeyDown(KeyCode.F2))
            {
                this.SendCommand(new CMD_SendHexData("a55a0505eeee"));
            }
        }

        void OnDisable()
        {
            // 关闭 串口通信
            this.SendCommand(new CMD_CloseSerialPort());
        }
#endif
    }
}